﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CvModel;
using DAL;

namespace LAB3_Cv
{
    public partial class AdministerAdmin : System.Web.UI.Page
    {
        Admin headHunter;
        Applicant applicant;
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!IsPostBack)
                {
                    BindData();
                    BindApplicant();
                }
            }
            catch (Exception)
            {
                throw;
            }
        }

        private void BindData()
        {
            GridViewHeadHunter.DataSource = AdminDB.GetHeadHunters();
            GridViewHeadHunter.DataBind();
        }

        private void BindApplicant()
        {
            GridViewApplicant.DataSource = ApplicantDB.GetApplicants();
            GridViewApplicant.DataBind();
        }

        protected void GridViewHeadHunter_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridViewHeadHunter.EditIndex = e.NewEditIndex;
            BindData();

        }

        protected void GridViewHeadHunter_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {

            int adminId = Int32.Parse(((Label)GridViewHeadHunter.Rows[e.RowIndex]
                        .FindControl("lblAdminId")).Text);

            bool isDeleted = ((CheckBox)GridViewHeadHunter.Rows[e.RowIndex]
                        .FindControl("chkIsDeleted")).Checked;
            foreach (var item in AdminDB.GetHeadHunters())
            {
                if (item.AdminId == adminId)
                {
                    item.IsDeleted = isDeleted;
                    headHunter = item;
                }
            }


            if (headHunter != null)
            {
                AdminDB.UpdateAdmin(headHunter);
            }
            GridViewHeadHunter.EditIndex = -1;
            BindData();
        }

        protected void AddNewHeadHunter(object sender, EventArgs e)
        {
            Admin headH = new Admin();
            headH.UserName = ((TextBox)GridViewHeadHunter.FooterRow.FindControl("txtUserName")).Text;
            headH.Password = ((TextBox)GridViewHeadHunter.FooterRow.FindControl("txtPassword")).Text;
            headH.CompanyName = ((TextBox)GridViewHeadHunter.FooterRow.FindControl("txtCompanyName")).Text;
            headH.Phone = ((TextBox)GridViewHeadHunter.FooterRow.FindControl("txtPhone")).Text;
            headH.Address = ((TextBox)GridViewHeadHunter.FooterRow.FindControl("txtAddress")).Text;
            headH.City = ((TextBox)GridViewHeadHunter.FooterRow.FindControl("txtCity")).Text;
            headH.Zip = ((TextBox)GridViewHeadHunter.FooterRow.FindControl("txtZip")).Text;
            headH.Email = ((TextBox)GridViewHeadHunter.FooterRow.FindControl("txtEmail")).Text;
            headH.AdminTitle = AdminTitle.Headhunter;

            AdminDB.CreateAdmin(headH);
            BindData();
        }

        protected void GridViewHeadHunter_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridViewHeadHunter.EditIndex = -1;
            BindData();
        }

        protected void GridViewApplicant_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridViewApplicant.EditIndex = -1;
            BindApplicant();
        }

        protected void GridViewApplicant_RowEditing(object sender, GridViewEditEventArgs e)
        {
            GridViewApplicant.EditIndex = e.NewEditIndex;
            BindApplicant();
        }

        protected void GridViewApplcant_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int applicantId = Int32.Parse(((Label)GridViewApplicant.Rows[e.RowIndex]
                        .FindControl("lblApplicantId")).Text);

            bool isDeleted = ((CheckBox)GridViewApplicant.Rows[e.RowIndex]
                        .FindControl("chkApplicantDeleted")).Checked;
            foreach (var item in ApplicantDB.GetApplicants())
            {
                if (item.ApplicantId == applicantId)
                {
                    item.IsDeleted = isDeleted;
                    applicant = item;
                }
            }


            if (applicant != null)
            {
                ApplicantDB.UpdateApplicant(applicant);
            }
            GridViewApplicant.EditIndex = -1;
            BindApplicant();
        }

    }


}